package org.jcodetools.http.request;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jcodetools.collection.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;

/**
 * 发送HTTP请求的工具类
 * 依赖于HttpClient，您需要在您maven项目中添加如下配置：
 * <dependency>
 * <groupId>org.apache.httpcomponents</groupId>
 * <artifactId>httpclient</artifactId>
 * <version>${httpclient.version}</version>
 * </dependency>
 * <p>
 * <div>Created by Administrator on 2017-09-28.</div>
 *
 * @since org.jcodetools.collection.CollectionUtils
 */
public class HttpClientUtils {
    private static HttpClient httpClient;
    private static String result;

    /**
     * 发送POST请求
     *
     * @param url     请求地址
     * @param map     请求参数
     * @param charset 编码符
     * @return 返回响应数据
     */
    public static String doPost(String url, Map<String, String> map, String charset) {
        HttpPost httpPost;
        try {
            httpClient = new SSLClient();
            httpPost = new HttpPost(url);
            //设置参数
            List<NameValuePair> list = new ArrayList<>();

            if (!CollectionUtils.isEmpty(map)) {
                for (Entry<String, String> entry : map.entrySet()) {
                    list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
                }
            }

            if (!CollectionUtils.isEmpty(list)) {
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);
                httpPost.setEntity(entity);
            }

            HttpResponse response = httpClient.execute(httpPost);
            if (!Objects.isNull(response)) {
                HttpEntity resEntity = response.getEntity();
                if (!Objects.isNull(resEntity)) {
                    result = EntityUtils.toString(resEntity, charset);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }

    /**
     * 发送GET请求
     *
     * @param url     请求地址
     * @param charset 编码符
     * @return 返回响应数据
     */
    public static String doGet(String url, String charset) {
        HttpGet httpGet;

        try {
            httpClient = new SSLClient();
            httpGet = new HttpGet(url);

            HttpResponse response = httpClient.execute(httpGet);
            if (!Objects.isNull(response)) {
                HttpEntity resEntity = response.getEntity();
                if (!Objects.isNull(resEntity)) {
                    result = EntityUtils.toString(resEntity, charset);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }
}
